using System;
using System.Collections.Generic;
using Kayac;
using UnityEngine;

public class Player : MonoBehaviour
{
	[Serializable]
	public class Settings
	{
		public PidSettings positionPid;

		public float gunPointDistance;

		public float bulletScaleFactor;

		public float recoil;

		public PidSettings headPid;

		public PidSettings hipsPid;

		public PidSettings upperLegPid;

		public PidSettings lowerLegPid;
	}

	[SerializeField]
	private Transform gunPointBase;

	[SerializeField]
	private Rigidbody head;

	[SerializeField]
	private Rigidbody hips;

	[SerializeField]
	private Rigidbody upperLegL;

	[SerializeField]
	private Rigidbody upperLegR;

	[SerializeField]
	private Rigidbody lowerLegL;

	[SerializeField]
	private Rigidbody lowerLegR;

	[SerializeField]
	private Rigidbody upperArmL;

	[SerializeField]
	private Rigidbody upperArmR;

	[SerializeField]
	private Rigidbody lowerArmL;

	[SerializeField]
	private Rigidbody lowerArmR;

	private Mesh bulletMesh;

	private BulletBase bulletPrefab;

	private List<BulletBase> bullets = new List<BulletBase>();

	private PidController3 positionPid;

	private Settings settings;

	private Vector3 basePosition;

	private BulletBase lastBullet;

	private Level.Settings levelSettings;

	private PidControllerRotation headPid;

	private PidControllerRotation hipsPid;

	private PidControllerRotation upperLegLPid;

	private PidControllerRotation upperLegRPid;

	private PidControllerRotation lowerLegLPid;

	private PidControllerRotation lowerLegRPid;

	public bool Alive { get; private set; }

	public Vector3 HeadPosition
	{
		get
		{
			if (head != null)
			{
				return head.position;
			}
			return Vector3.zero;
		}
	}
	public Transform HipsTransform
	{
		get
		{
			if (hips != null)
			{
				return hips.transform;
			}
			return null;
		}
	}
	public Quaternion HeadRotation => default(Quaternion);

	private void OnBulletKilled(BulletBase bullet)
	{
		bullets.Remove(bullet);
	}
	public void ManualStart(Settings settings, Level.Settings levelSettings, BulletBase bulletPrefab, Dictionary<string, Material> materialSet)
	{
		this.settings = settings;
		this.bulletPrefab = bulletPrefab;
		Alive = true;
	}

	public float CalcBulletRadiusFromScreenLength(float screenLength)
	{
		float scale = screenLength / Screen.width * 2;
		const float minScale = 0.05f;
		const float maxScale = 1.0f;
		return Mathf.Clamp(scale, minScale, maxScale);
	}
	public void ManualFixedUpdate(float deltaTime, bool pointerDown, Vector2 pointer)
	{
		if (bullets != null)
		{
			for (int i = 0; i < bullets.Count; i++)
			{
				bullets[i].ManualFixedUpdate(deltaTime);
			}
		}
	}

	public void ManualUpdate(float deltaTime)
	{
		if (bullets != null)
		{
			foreach (Bullet bullet in bullets)
			{
				bullet.ManualUpdate(deltaTime);
			}
		}
	}

	public Vector3? GetLastBulletTarget()
	{
		return null;
	}

	public void CollectPartPoints(List<Vector3> points)
	{
	}

	public void Fire(EnemyPart target, Vector3 localTargetPoint, Vector3 targetPoint0, Vector3 targetPoint1, Transform bulletHolder, ParticleRenderer particleRenderer, float screenLength, Vector3 rayOrigin, float hitDistance)
	{
		GameObject bulletObject = PoolManager.Instance.Obtain(PoolType.Thrown);
		bulletObject.transform.SetParent(bulletHolder);
		bullets.Add(bulletObject.GetComponent<BulletBase>());
		float scale = CalcBulletRadiusFromScreenLength(screenLength);
		if (scale > 0.2f)
		{
			AudioManager.Instance.PlayOnShot((int)ClipType.touchu);
		}
		else
		{
			AudioManager.Instance.PlayOnShot((int)ClipType.dianjifashe);
		}
		Vector3 hitPoint = rayOrigin + targetPoint0.normalized * hitDistance;
		targetPoint0 = (hitPoint - gunPointBase.position).normalized;
		Vector3 upVector = Vector3.Cross(targetPoint0, targetPoint1);
		bulletObject.GetComponent<BulletBase>().ManualStart(target, localTargetPoint, upVector, gunPointBase.position, targetPoint0, particleRenderer, bulletMesh, scale);
		bulletObject.GetComponent<BulletBase>().OnKilled += OnBulletKilled;
	}

	public void Die(Vector3 velocity)
	{
		Alive = false;
		AudioManager.Instance.PlayOnShot((int)ClipType.shibai);
		hips.AddForce(velocity * 10f, ForceMode.Impulse);
		head.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		head.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperLegL.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperLegL.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperLegR.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperLegR.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerLegL.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerLegL.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerLegR.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerLegR.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperArmL.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperArmL.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperArmR.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		upperArmR.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerArmL.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerArmL.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerArmR.GetComponent<ConfigurableJoint>().angularXDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		lowerArmR.GetComponent<ConfigurableJoint>().angularYZDrive = new JointDrive
		{
			positionSpring = 0f,
			positionDamper = 0f,
			maximumForce = 0f
		};
		Main.Instance.gameStatus = GameStatus.GameFail;
	}

	private void ControlBody(float deltaTime)
	{
	}
}
